/*
 *  map.c
 *  OpenGL-PingPongCpp
 *
 *  Created by wu di on 12-7-8.
 *  Copyright 2012 UTS. All rights reserved.
 *
 */

// make changes to xcode 4.6

#include "map.h"

int blocks[8][4][16] =  
{
// |
  {{  1,1,1,1,  0,0,0,0,  0,0,0,0,  0,0,0,0},
   {  0,1,0,0,  0,1,0,0,  0,1,0,0,  0,1,0,0},
   {  1,1,1,1,  0,0,0,0,  0,0,0,0,  0,0,0,0},
   {  0,1,0,0,  0,1,0,0,  0,1,0,0,  0,1,0,0}
  },
// j  
  {
  {0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0},
  {1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0},
  {1,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0},
  {1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0}
  },
  // l
  {
  {1,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0},
  {0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0},
  {1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0},
  {1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0}
  },
  // o
  {
  {1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
  {1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
  {1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
  {1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0}
  },
  // s
  {
  {0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},
  {1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0},
  {0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},
  {1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0}
  },
  // z
  {
  {1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0},
  {1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0}
  },
  // k
  {
  {1,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0},
  {0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0},
  {1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0}
  }
};

int xshape = column/2;
int yshape = row - 5;
int type = 0;//type = 0 - 8
int state = 0;//state = 0 - 4

typedef enum
  {
    RandomGenerator,
    Droping,
    Finished
  }DropingState;

typedef enum
  {
    TRUE,
    FALSE
  }BOOL;

DropingState dropingState = Droping; // 0 means droping,1 means finished

int map[row][column] = {0};

void cleanMatrix()
{
  int i,j;
  for(i = 0;i<row;i++)
      for(j = 0;j<column;j++)
        map[i][j] = 0;
}

void initMatrix()
{
  // if map[i][j] == 2 means this map is block  
  int i,j;
  for(i=0;i<row;i++)
    {
      for(j = 0;j<column;j++)
        {
          if(i == 0||i == row - 1)
            map[i][j] = 2;
          if(j == 0||j == column  - 1)
            map[i][j] = 2;
        }
    }

#ifdef DEBUG    
  for(i=0;i<row;i++)
    {
      for(j = 0;j<column;j++)
        {
          printf("%d ",map[i][j]);
        }
      printf("\n");
    }
#endif  
}


void drawMatrixRectf(int x,int y)
{
  /*
    startx+10*x
    starty+10*y
   */
  glColor3f(x/10,y/10,0);
  glRectf(blocksize*x - xoffset,blocksize*y - yoffset,
          blocksize*x + blocksize - xoffset,blocksize*y + blocksize - yoffset);
}

int ToMapX(int x)
{
  
}

int ToMapY(int y)
{
  
}

void drawBlock()
{
  int i,j;
  for(i=0;i<row;i++)
    {
      for(j = 0;j<column;j++)
        {
          if(map[i][j] == 2) 
            drawMatrixRectf(j,i);
        }
    }
}

void drawSmallBlock(int x,int y)
{
  glColor3f(1.0f, 0.5f, 0.0f);
  glRectf(blocksize*x + blocksize*xshape - xoffset,blocksize*(3 - y) + blocksize*yshape - yoffset,
          blocksize*(x + 1) + blocksize*xshape - xoffset,blocksize*(4 - y) + blocksize*yshape - yoffset);
}

//if block is conflict with map, this function will reture TRUE,
//if everything is ok, then return FALSE
BOOL conflict(int x,int y,int t,int s)
{
  int i,j;
  int *p = blocks[t][s];
  for(i=0;i<4;i++)
    for(j=0;j<4;j++)
      {
        if(3 - i + x >= 0 && j + y >= 0)
          {
            if(p[4*i + j]!= 0 && map[3 - i+x][j+y] != 0)
              return TRUE;
          }
      }
 
  return FALSE;
}

void initShape()
{
  dropingState = RandomGenerator;
  time_t t;
  srand((unsigned) time(&t));
  type = rand()%7;
  state = rand()%4;
  xshape = column/2;
  yshape = row - 5;
}

void drawShape()
{
  int i,j;
  int *p = blocks[type][state];
  if(dropingState == RandomGenerator)
    {
      
    }
  else if(dropingState == Droping)
    {
      for(i = 0;i<4;i++)
        for(j = 0;j<4;j++)    
          {
            if(p[i + 4*j] == 1) // i and j revert
              drawSmallBlock(i,j);
          }
    }
}

void updateShape()
{
  int futurePositionY = yshape-1;      
  if(conflict(futurePositionY,xshape,type,state) == FALSE)
    yshape = futurePositionY;
  // xshape--;
}


void ShapeChangeState()
{
  int futureState = (state + 1)%4;
  if(conflict(yshape,xshape,type,futureState) == FALSE)
    state = futureState;
  glutPostRedisplay();
}

void MoveLeft()
{
  int futurePositionX = xshape - 1;
  if(conflict(yshape,futurePositionX,type,state) == FALSE)
    xshape = futurePositionX;
  glutPostRedisplay();
}

void MoveRight()
{
  int futurePositionX = xshape + 1;
  if(conflict(yshape,futurePositionX,type,state) == FALSE)
    xshape = futurePositionX;
  glutPostRedisplay();  
}

void MoveDown()
{
  int futurePositionY = yshape - 1;
  if(conflict(futurePositionY,xshape,type,state) == FALSE)
    yshape = futurePositionY;
  glutPostRedisplay();
}

/*
  blocks[8][4][16]

int type
srand(x*y%8);
type=random()%8;
state=random()%4;

int *target=blocks[type][state];


      
+---------------------+
|        |            | column-1
|        |            |         
|-----x  |            |    i+(N - xsharp)
|     |  |            |    j+ysharp
|     |               |            
|     |               |            
|     |               |            
|     |               | 1          
+---------------------+            
(0,0)                              

int blockOffsetX;
int blockOffsetY;

drawBlocks(blockOffsetX,blockOffsetY);

//map

int positionInMap(int x,int y,int blockOffsetX,int blockOffsetY)
{
  
}

drawBlocks(blockOffsetX,blockOffsetY)
{
  int i,j;
  for(i = 0;i<blockOffsetX;i++)
    for(j = 0;j<blockOffsetX;j++)
      {
        
      }
}


conflict()

for(i=0;i<4;i++)
  for(j=0;j<4;i++)
  map[x+xshape][y+yshape] != 0

 */
